package com.sample.ab;

/**
 * @author 张智研
 * @date 2024/09/27 14:16
 **/
import java.util.*;
import java.util.stream.*;

class Item {
    private String category;
    private String name;

    public Item(String category, String name) {
        this.category = category;
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Item{category='" + category + "', name='" + name + "'}";
    }
}

public class GroupListExample {
    public static void main(String[] args) {
        List<Item> items = Arrays.asList(
                new Item("a", "television"),
                new Item("a", "computer"),
                new Item(null, "novel"),
                new Item("sss", "dictionary")
        );

        Map<String, List<Item>> groupedItems = items.stream().filter(item -> item.getCategory() != null)
                .collect(Collectors.groupingBy(Item::getCategory));

        groupedItems.forEach((category, itemsInCategory) -> {
            System.out.println(category + ":");
            itemsInCategory.forEach(System.out::println);
        });
    }
}
